package com.sky.controller.admin;

import com.sky.dao.CategoryDTO;
import com.sky.dao.CategoryPageDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "后台-分类管理")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    @ApiOperation("分類查詢")
    public Result<PageResult> page(CategoryPageDTO categoryPageDTO) {
        PageResult pageResult = categoryService.page(categoryPageDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    @ApiOperation("根據類型查詢分類")
    public Result<List<Category>> list(Integer type) {
        List<Category> categoryList = categoryService.list(type);
        return Result.success(categoryList);
    }

    @ApiOperation("修改分類")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        categoryService.update(categoryDTO);
        return Result.success();
    }

    @ApiOperation("啓用禁用分類")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, @PathParam("id") Integer id) {
        categoryService.startOrStop(status, id);
        return Result.success();
    }

    @ApiOperation("刪除分類")
    @DeleteMapping
    public Result delete(Integer id) {
        categoryService.delete(id);
        return Result.success();
    }

    @ApiOperation("新增分類")
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        categoryService.save(categoryDTO);
        return Result.success();
    }
}
